<?php

/**
 * list_drink actions.
 *
 * @package    potaskuy
 * @subpackage list_drink
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class list_drinkActions extends sfActions
{

  public function getFirstCat()
  {
  	$q = Doctrine_Query::create()
	->from('category c')
	->where('c.type = ?', '2')
	->addOrderBy('c.c_order')
	->limit(1);
	$temps = $q->execute();
	
	$id = $temps[0]->getId();

	return $id;
  }
  
   public function getFirstFood($cat)
  {
  	$q = Doctrine_Query::create()
	->from('food f')
	->where('f.category = ?', $cat)
	->addOrderBy('f.f_order')
	->limit(1);
	$temps = $q->execute();
	return $temps[0];
  }

  public function executeIndex(sfWebRequest $request)
  {
  	if ($request->getParameter('cat'))
	{
		$q = Doctrine_Query::create()
		->from('food f')
		->where('f.category = ?', $request->getParameter('cat'))
		->addOrderBy('f.f_order');
		$this->category = $request->getParameter('cat');
	}
	else
	{
		$q = Doctrine_Query::create()
		->from('food f')
		->where('f.category = ?', $this->getFirstCat())
		->addOrderBy('f.f_order');
		$this->category = $this->getFirstCat();
	}
	$this->foods = $q->execute();
	
	$this->menus = Doctrine_Query::create()
      ->from('category c')
	  ->where('c.type = ?', '2')
	  ->addOrderBy('c.c_order')
      ->execute();
	
	$this->catname = Doctrine_Core::getTable('Category')
	->find($this->category)
	->getName();
	
	if ($request->getParameter('id'))
	{
		$this->currentfood = Doctrine_Core::getTable('Food')
		->find($request->getParameter('id'));
	}
	else
	{
		$this->currentfood = $this->getFirstFood($this->category);
	}
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->food = Doctrine_Core::getTable('Food')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->food);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new FoodForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new FoodForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($food = Doctrine_Core::getTable('Food')->find(array($request->getParameter('id'))), sprintf('Object food does not exist (%s).', $request->getParameter('id')));
    $this->form = new FoodForm($food);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($food = Doctrine_Core::getTable('Food')->find(array($request->getParameter('id'))), sprintf('Object food does not exist (%s).', $request->getParameter('id')));
    $this->form = new FoodForm($food);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($food = Doctrine_Core::getTable('Food')->find(array($request->getParameter('id'))), sprintf('Object food does not exist (%s).', $request->getParameter('id')));
    $food->delete();

    $this->redirect('list_drink/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $food = $form->save();

      $this->redirect('list_drink/edit?id='.$food->getId());
    }
  }
}
